<?php
namespace voxfa\net\store\pdo;

use voxfa\net\NetworkInterface;
use voxfa\net\InetAddress;
use voxfa\net\store\NetworkInterfaceProvider;

class NetworkInterfaceStore implements NetworkInterfaceProvider,NetworkInterfaceConstants {
	
	protected $prefix;
	protected $con;
	
	public function __construct($dsn,$user = null,$pass = null,$options = array(),$prefix="voxfa_"){
		$this->con = new PDO($dsn,$user,$pass,$options);
		$this->prefix = $prefix;
	}	
	
	public function insertInterface(NetworkInterface $interface){
		$sql=sprintf(self::INSERT_INTERFACE,$this->prefix.self::TABLE);
		$stmt=$this->con->prepare($sql);
		$stmt->bindValue(":dev",$interface->getName());
		$stmt->bindValue(":name",$interface->getDisplayName());
		$stmt->bindValue(":attr",$this->formatterAttributes($interface->getAttributes()));
		$stmt->execute();
	}
	
	public function deleteInterface(NetworkInterface $interface){
		$sql=sprintf(self::DELETE_INTERFACE,$this->prefix.self::TABLE);
		$stmt=$this->con->prepare($sql);
		$stmt->bindValue(":dev",$interface->getName());
		$stmt->execute();
	}
	
	public function updateInterface(NetworkInterface $interface){
		$sql=sprintf(self::UPDATE_INTERFACE,$this->prefix.self::TABLE);
		$stmt=$this->con->prepare($sql);
		$stmt->bindValue(":dev",$interface->getName());
		$stmt->bindValue(":attr",$this->formatterAttributes($interface->getAttributes()));
		$stmt->bindValue(":name",$interface->getDisplayName());
		$stmt->execute();
	}
	
	public function existInterface($interfaceName){
		$sql=sprintf(self::EXIST_INTERFACE,$this->prefix.self::TABLE);
		$stmt=$this->con->prepare($sql);
		$stmt->bindValue(":dev",$interfaceName);
		$stmt->execute();
		return $stmt->fetchColumn(0)>0;
	}
	
	public function selectByName($interfaceName){
		$sql=sprintf(self::SELECT_BY_NAME,$this->prefix.self::TABLE);
		$stmt=$this->con->prepare($sql);
		$stmt->bindValue(":dev",$interfaceName);
		$stmt->execute();
		$result=$stmt->fetch(PDO::FETCH_ASSOC);
		
		return $this->hydrateObject($result);
	}
	
	public function selectAll(){
		$sql=sprintf(self::SELECT_BY_NAME,$this->prefix.self::TABLE);
		$stmt=$this->con->prepare($sql);
		$stmt->execute();
		$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$v = array();
		foreach($results as $result){
			$v[]=$this->hydrateObject($result);
		}
		return $v;
	}
	
	private function hydrateObject($result){
		$new = new NetworkInterface();
		$new->setName($result['name']);
		$new->setDisplayName($result['displayName']);
		$new->setAttributes($this->parseAttributes($result['attributes']));
		return $new;
	}
	
	private function formatterAttributes($params){
		$v = array();
		foreach($params as $key=>$value){
			$v[]=$key.":".$value;
		}
		return implode(";",$v);
	}
	
	private function parseAttributes($paramString){
		$params = array();
		$v = explode(";",$paramString);
		foreach($v as $unit){
			list($key,$value)=explode(":",$unit);
			$params[$key]=$value;
		}
		return $params;
	}
}